package com.example.gybicycle.service;

import java.util.*;

import com.example.gybicycle.common.entity.Result;
import com.example.gybicycle.common.enums.NumType;
import com.example.gybicycle.common.sql.CrudService;
import com.example.gybicycle.common.util.LoginUtil;
import com.example.gybicycle.common.util.NumberUtil;
import com.example.gybicycle.common.util.TreeUtil;
import com.example.gybicycle.dao.SysMenuDao;
import com.example.gybicycle.entity.*;
import com.example.gybicycle.entity.SysMenu;
import com.example.gybicycle.dao.SysUserDao;
import jakarta.servlet.http.HttpServletRequest;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

@Service
public class SysUserService extends CrudService<SysUserDao, SysUser> {
    @Autowired
    private SysMenuDao menuDao;
    public Result<LoginInfo> login(SysUser user, HttpServletRequest request){
//        LoginInfo info=LoginUtil.getLoginInfo(request);
//        if(info==null||info.getUser()==null||info.getUser().getCode()==null||user.getCode()==null)
//            return Result.fail("未获取到验证码");
//        if(!info.user.getCode().toLowerCase().equals(user.getCode().toLowerCase()))
//            return Result.fail("验证码错误");
        LoginInfo info=new LoginInfo();
        if(StringUtil.isBlank(user.getName())||StringUtil.isBlank(user.getPassword()))
            return Result.fail("账号密码不能为空");
        user=dao.login(user.getName(),user.getPassword());
        if(user==null)
            return Result.fail("账号或密码错误");
        if(user.getFrozen()!=null&&user.getFrozen()==false)
            return Result.fail("账号已被冻结，请联系管理员解冻");
       if(user.getRoleId().equals("0"))
           user.setRoleName("超级管理员");
        info.setUser(user);
        info.setRoleName(user.getRoleName());
        info=getMenu(info);
        info.setAuthorization(UUID.randomUUID().toString());
        LoginUtil.setLoginInfo(info);
        return Result.success(info);
    }
    @Override
    public SysUser get(String id) {
        SysUser table=super.get(id);
        return table;
    }

    public LoginInfo getMenu(LoginInfo info){
        List<SysMenu> list=new ArrayList<>();
        if(info.getUser().getRoleId().equals("0")){
            SysMenu menu=new SysMenu();
            menu.setVisible(0);
            list= menuDao.findList(new SysMenu());
        }else {
            list=menuDao.findByRole(info.user.getRoleId());
        }
        if(list==null &&list.size()==0)
            return info;
        List<String> perssion=list.stream().map(SysMenu::getPerm).collect(Collectors.toList());
        perssion=perssion.stream().filter(Objects::nonNull).filter(string ->!string.trim().isEmpty()).distinct().collect(Collectors.toList());
        info.setPermissions(perssion);
        List<SysMenu> url=list.parallelStream().filter(SysMenu->!SysMenu.getType().equals(3)).collect(Collectors.toList());
        List<SysMenuData> tree=TreeUtil.menuToTree(url);
        info.setMenuList(url);
        info.setTreeMenuList(tree);
        return info;
    }

    public List<SysMenuData> findMenu(LoginInfo info){
        List<SysMenu> list=new ArrayList<>();
        if(info.getUser().getRoleId().equals("0")){
            SysMenu menu=new SysMenu();
            menu.setVisible(0);
            list= menuDao.findList(new SysMenu());
        }else {
            list=menuDao.findByRole(info.user.getRoleId());
        }
        List<SysMenuData> tree=TreeUtil.menuToTree(list);
        return tree;
    }
    public Result save(SysUser entity,String loginId){
        int num=0;
        if(entity.getId()==null){
           
            num=dao.insert(entity);
        }else {
           
            num=dao.update(entity);
        }
        if(num>0)
            return Result.success(entity);
        else 
            return Result.fail("保存失败");
    }

    @Override
    public int insert(SysUser entity) {
        entity.setId(NumberUtil.getNum(NumType.USER));
        if(entity.getFrozen()==null)
            entity.setFrozen(true);
        int num=super.insert(entity);
        return  num;
    }

    @Override
    public int insertByList(List<SysUser> list) {
        for(SysUser entity:list){
            entity.setId(NumberUtil.getNum(NumType.USER));
        }
        return super.insertByList(list);
    }

    public List<SysUser> findList(SysUser entity){
        List<SysUser> list=super.findList(entity);
        return list;
    }
    public SysUser getByOpenId(String openid){
        return dao.getByOpenId(openid);
    }
    public SysUser getByTelphone(String telphone){
        return dao.getByTelphone(telphone);
    }


}